package com.spiro.ssl;

import com.spiro.common.BaseJavaServer;
import com.spiro.common.Configuration;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TServerTransport;

/**
 * Created by tz0643 on 2016/6/28.
 */
public class MultiThreadSslBioServer extends BaseJavaServer {

    private int maxWorkerThread = 8;

    public int getMaxWorkerThread() {
        return maxWorkerThread;
    }

    public void setMaxWorkerThread(int maxWorkerThread) {
        this.maxWorkerThread = maxWorkerThread;
    }

    public MultiThreadSslBioServer(int port) {
        super(port);
    }

    @Override
    public void serve() {
        try {
            TSSLTransportFactory.TSSLTransportParameters params
                    = new TSSLTransportFactory.TSSLTransportParameters();

            String keystoreFilename = Configuration.getInstance()
                    .getConf("ssl.keystore.filename");
            String keystorePassword = Configuration.getInstance()
                    .getConf("ssl.keystore.password");

            // The Keystore contains the private key
            params.setKeyStore(keystoreFilename, keystorePassword, null, null);

            TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(port, 0, null, params);

            TServer server = new TThreadPoolServer(
                    new TThreadPoolServer.Args(serverTransport)
                            .maxWorkerThreads(this.maxWorkerThread).processor(processor));

            System.out.println("Starting the secure server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
